package com.hboxs.asl.entity;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by hboxs002 on 2016/10/27.
 * Entity - 商品浏览记录
 */
@Entity
@Table(name = "xx_view_history")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_view_history_sequence")
public class ViewHistory extends BaseEntity {

    /**
     * 浏览商品的会员
     */
    private Member member;

    /**
     * 被浏览的商品
     */
    private Product product;

    /**
     * 浏览商品的时间(即进入详情页的时间)
     */
    private Date date;

    public ViewHistory() {
        super();
    }

    public ViewHistory(Member member, Product product, Date date) {
        this.member = member;
        this.product = product;
        this.date = date;
    }

    /**
     * 获取会员
     * @return
     */
    @ManyToOne(fetch = FetchType.LAZY)
    public Member getMember() {
        return member;
    }

    /**
     * 设置会员
     * @param member
     */
    public void setMember(Member member) {
        this.member = member;
    }

    /**
     * 获取被浏览的商品
     * @return
     */
    @ManyToOne(fetch = FetchType.LAZY)
    public Product getProduct() {
        return product;
    }

    /**
     * 设置被浏览的商品
     * @param product
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    /**
     * 获取浏览商品的时间
     * @return
     */
    public Date getDate() {
        return date;
    }

    /**
     * 设置浏览商品的时间
     * @param date
     */
    public void setDate(Date date) {
        this.date = date;
    }

    /**
     * 转化时间格式
     * @return
     */
    @Transient
    public String getViewDate(){
        Date date = getDate();
        if (date != null){
            return new SimpleDateFormat("yyyy-MM-dd").format(date);
        }
        return null;
    }
}
